package pacote;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Connect {
    public void open(){
       try{
           Class.forName("org.gjt.mm.mysql.Driver");
           con = DriverManager.getConnection(url, user, pass);
           stmt = con.createStatement();
       }catch(ClassNotFoundException ex){
           System.out.println(ex.getMessage());
       }catch(SQLException ex){
           System.out.println(ex.getMessage());
       }
       System.out.println("Sucesso");
   
    
    }
    
    public void close(){
        try{
            stmt.close();
            con.close();
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
        }
        System.out.println("Conexão Fechada");
    }
    
    public void execute(String query){
        try{
            if (stmt.executeUpdate(query) != Statement.EXECUTE_FAILED){
                
                System.out.println("Registro feito com sucesso");
            }
            
        }catch(SQLException ex){
            
            System.out.println(ex.getMessage());
        }
    }
    public ResultSet run (String query){
        try {
            open();
            rs = stmt.executeQuery(query);
            
            
        }catch (SQLException ex){
            System.out.println(ex.getMessage());
                    
        }
        return rs;
    }
    public String[] selectLivro(int id){
        open();
        String  dados[] = new String [10];
        String query = "SELECT * FROM ´LIVRO´ WHERE ´id´="  + id;      
        try{
            rs = stmt.executeQuery(query);
            while(rs.next()){
                dados[0] = "" + rs.getInt("id");
                dados[1] = "" + rs.getString("titulo");
                dados[2] = "" + rs.getString("stitulo");
                dados[3] = "" + rs.getString("grupo");
                dados[4] = "" + rs.getString("autor");
                dados[5] = "" + rs.getString("idioma");
                dados[6] = "" + rs.getString("assunto");
                dados[7] = "" + rs.getString("ilustrado");
                dados[8] = "" + rs.getString("colorido");
                dados[9] = "" + rs.getString("resenha");
                
            }
        }catch(SQLException ex){
            System.err.println(ex.getMessage());
            
        }finally{
            close();
        }return dados;
    }
    
    private final String url = "jdbc:mysql://localhost:3306/4teca";
    private final String user = "root";
    private final String pass = "";
    
    Connection con;
    Statement stmt;
    ResultSet rs;

    String[] selectLivro(String query) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}